<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数组的平方</title>
</head>
<body>
  <script>
    function fn(nums) {
      const n = nums.length
      const res = new Array(n).fill(0)
      let l = 0
      let r = n - 1
      let i = n - 1
      while (l <= r) {
        if (nums[l] * nums[l] > nums[r] * nums[r]) {
          res[i--] = nums[l] * nums[l++]
        } else {
          res[i--] = nums[r] * nums[r--]
        }
      }
      return res
    }
    console.log(fn([-4, -1, 0, 3, 10]))
  </script>
</body>
</html>